package Q202;

import java.util.HashSet;
import java.util.Set;

/**
 * 快乐数
 *
 * @author 23737
 * @time 2021.12.03
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution2 {
    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        while (n != 1 && !record.contains(n)) {
            record.add(n);
            n = getNextNumber(n);
        }
        return n == 1;
    }

    private int getNextNumber(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n = n / 10;
        }
        return res;
    }
}
